const defaultState = {
    feeList: [],
    homeFee: 0,
    sheetFee: 0,
    homeCount: 0,
    sheetCount: 0,
    currency: undefined,
    sheetCurrency: undefined,
    overFee: 0,
    overFeeCurrency: undefined

};
export default function orderFee(state = defaultState, action) {
    const {type, payload} = action;
    let obj = null;
    switch (type) {
        case 'ZAD_ORDER.UPDATE_ORDER_FEE':
            if (payload && payload.length > 0) {
                payload.forEach(item => {
                    const found = state.feeList.find(f => f.feeName === item.feeName);
                    if (!found) {
                        state.feeList.push({feeName: item.feeName, status: "UNLOCK"});
                    }
                });
            }
            // 更新feeList
            if (payload && payload.length > 0) {
                state.feeList.forEach(fee => {
                    const found = payload.find(f => f.feeName === fee.feeName);
                    if (found) {
                        fee.feeName = found.feeName;
                        fee.feeCount = found.feeCount;
                        fee.currency = found.currency;
                        fee.status = found.status;
                        fee.canSee = found.canSee;
                    }
                });
            }
            const fees = [];
            state.feeList.forEach(fee => {
                fees.push({...fee, id: Math.random().toString()});
            });
            obj = null;
            // 把运输费排到最前面
            fees.forEach((item, index) => {
                if (item.feeName === '运输费') {
                    obj = item;
                    fees.splice(index, 1);
                    return;
                }
            });
            if(obj !== null){
                fees.unshift(obj);
            }
            return {...state, feeList: fees};
        case 'ZAD_ORDER.CALCULATE_FEE':
            // 更新feeList
            if (payload && payload.length > 0) {
                state.feeList.forEach(fee => {
                    const found = payload.find(f => f.feeName === fee.feeName);
                    if (found && "IN" === found.type) {
                        fee.feeName = found.feeName;
                        fee.feeCount = found.feeCount;
                        fee.currency = found.currency;
                        fee.canSee = found.canSee;
                    }
                });
            }
            const fees2 = [];
            state.feeList.forEach(fee => {
                fees2.push({...fee, id: Math.random().toString()});
            });
            obj = null;
            // 把运输费排到最前面
            fees2.forEach((item, index) => {
                if (item.feeName === '运输费') {
                    obj = item;
                    fees2.splice(index, 1);
                    return;
                }
            });
            if(obj !== null){
                fees2.unshift(obj);
            }
            return {...state, feeList: fees2};
        case 'ZAD_ORDER.GET_CLIENT_FEES':
            if (payload && payload.length > 0) {
                state.feeList.forEach(fee => {
                    const found = payload.find(f => f.feeName === fee.feeName);
                    if (found && fee.feeCount === 0) {
                        fee.feeName = found.feeName;
                        fee.feeCount = found.feeCount;
                        fee.currency = found.currency;
                        fee.status = found.status;
                        fee.canSee = found.canSee;
                    } else {
                        fee.feeCount = 0;
                        fee.currency = undefined;
                        fee.canSee = true;
                    }
                });
            }
            const fees3 = [];
            state.feeList.forEach(fee => {
                fees3.push({...fee, id: Math.random().toString()});
            });
            obj = null;
            // 把运输费排到最前面
            fees3.forEach((item, index) => {
                if (item.feeName === '运输费') {
                    obj = item;
                    fees3.splice(index, 1);
                    return;
                }
            });
            if(obj !== null){
                fees3.unshift(obj);
            }
            return {...state, feeList: fees3};
        case 'ZAD_ORDER.HIDE_EDIT':
        case 'ZAD_ORDER.HIDE_CREATE':
            return {
                ...state, feeList: [], homeFee: 0,
                sheetFee: 0,
                homeCount: 0,
                sheetCount: 0,
                overFee: 0,
                overFeeCurrency: undefined
            };
        case 'ZAD_ORDER.SHOW_EDIT':
            const orderFeeList = payload.feeList || [];
            if (payload && payload.length > 0) {
                state.feeList.forEach(fee => {
                    const found = orderFeeList.find(f => f.feeName === fee.feeName);
                    if (found) {
                        fee.feeName = found.feeName;
                        fee.feeCount = found.feeCount;
                        fee.currency = found.currency;
                        fee.status = found.status;
                        fee.canSee = found.canSee;
                    }
                });
            }
            return {...state};
        case 'ZAD_ORDER.GET_FEE_LIST':
            const feeList = [];
            if (payload && payload.length > 0) {
                payload.forEach(item => feeList.push({
                    id: Math.random().toString(),
                    feeName: item.name,
                    canSee: true,
                    feeCount: 0
                }));
            }
            return {...state, feeList: [...feeList]};
        case 'ZAD_ORDER.UPDATE_FEE_LIST':
            return {...state, feeList: [...payload]};
        case 'ZAD_ORDER.UPDATE_OVER_FEE':
            return {...state, overFee: payload.overFee, overFeeCurrency: payload.overFeeCurrency};
        case 'ZAD_ORDER.GET_BAOGUAN_FEE':
            return {
                ...state,
                homeFee: payload.homeFee,
                sheetFee: payload.sheetFee,
                currency: payload.currency,
                sheetCurrency: payload.sheetCurrency
            };
        case 'ZAD_ORDER.UPDATE_COUNT':
            return {...state, homeCount: payload.homeCount, sheetCount: payload.sheetCount};
        default:
            return state;
    }
}

